package com.goodwill.androidtest.sort;

public class SelectSort<T> implements ISort<T> {

    @Override
    public <T extends Comparable<? super T>> void sort(T[] list) {

        int index;                          //保存每次比较,最大值的下标;

        for (int i = 1; i < list.length; i++) {   //控制外循环次数
            index = 0;
            for (int j = 1; j <= list.length - i; j++) {
                if (list[j].compareTo(list[index]) == 1) {
                    index = j;
                }
            }
            swap(list, index, list.length - i);
        }
    }


    private static void swap(Comparable[] a, int x, int y) {
        Comparable tmp = a[x];
        a[x] = a[y];
        a[y] = tmp;
    }
}
